package com.salton123.feature

import android.text.TextUtils
import android.view.View
import com.salton123.arch.view.ITitleBarView
import com.salton123.arch.view.TitleBarStyle
import com.salton123.soulove.app.R
import com.salton123.ui.base.IComponentLife
import com.wuhenzhizao.titlebar.widget.CommonTitleBar

/**
 * Time:8/30/21 11:06 AM
 * Author:
 * Description:
 */
class TitleBarViewFeature : IFeature {
    private lateinit var host: Any
    private lateinit var callback: (titleBar: CommonTitleBar) -> Unit
    fun with(host: Any, callback: (titleBar: CommonTitleBar) -> Unit) {
        this.host = host
        this.callback = callback
    }

    override fun onBind() {
        if (host is IComponentLife && host is ITitleBarView) {
            (host as IComponentLife).inflateTitleBar(true, R.layout.salton_common_title_bar) {
                it?.findViewById<CommonTitleBar>(R.id.commonTitleBar)?.let { commonTitleBar ->
                    onInflateTitleBar(host as ITitleBarView, commonTitleBar)
                    callback.invoke(commonTitleBar)
                }
            }
        } else {
            throw RuntimeException("host must be instance of IComponentLife and ITitleBarView")
        }
    }

    override fun onUnBind() {
    }

    private fun onInflateTitleBar(impl: ITitleBarView, titleBar: CommonTitleBar) {
        if (TextUtils.isEmpty(impl.getTitleText())) {
            titleBar.centerTextView?.visibility = View.GONE
        } else {
            titleBar.centerTextView?.visibility = View.VISIBLE
            titleBar.centerTextView?.text = impl.getTitleText()
        }
        when (impl.getLeftStyle()) {
            TitleBarStyle.ICON_TEXT -> {   //icon & text
                titleBar.leftTextView?.text = impl.getLeftText()
                titleBar.leftTextView?.visibility = View.VISIBLE
                impl.getLeftIcon()?.let {
                    titleBar.leftTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(it, 0, 0, 0)
                }
            }
            TitleBarStyle.ICON -> {    //icon
                titleBar.leftTextView?.text = ""
                titleBar.leftTextView?.visibility = View.VISIBLE
                impl.getLeftIcon()?.let {
                    titleBar.leftTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(it, 0, 0, 0)
                }
            }
            else -> {    //hidden
                titleBar.leftTextView?.visibility = View.GONE
            }
        }

        when (impl.getRightStyle()) {
            TitleBarStyle.TEXT -> {   //icon & text
                titleBar.rightTextView?.text = impl.getRightText()
                titleBar.rightTextView?.visibility = View.VISIBLE
                impl.getRightIcon()?.let {
                    titleBar.rightTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(it, 0, 0, 0)
                }
            }
            TitleBarStyle.ICON -> {    //icon
                titleBar.rightTextView?.text = ""
                titleBar.rightTextView?.visibility = View.VISIBLE
                impl.getRightIcon()?.let {
                    titleBar.rightTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(it, 0, 0, 0)
                }
            }
            else -> {    //hidden
                titleBar.rightTextView?.visibility = View.GONE
            }
        }
        titleBar.setListener { v, action, _ ->
            when (action) {
                CommonTitleBar.ACTION_LEFT_TEXT -> {
                    impl.onLeftClick(v)
                }
                CommonTitleBar.ACTION_RIGHT_TEXT -> {
                    impl.onRightClick(v)
                }
            }
        }
    }
}